    mui.init({});
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var buy_currentPage=[1,1,1,1];     //买入
    var sale_currentPage=[1,1,1,1];     //卖出
    var pageSize=10;
    var buy_total_page=[0,0,0,0];
    var sale_total_page=[0,0,0,0];
    var buy_total_count=[0,0,0,0];
    var sale_total_count=[0,0,0,0];

    //阻尼系数
	var deceleration = mui.os.ios ? 0.003 : 0.0009;
	mui('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: false, //是否显示滚动条
		deceleration: deceleration
	});
	
	refresh_data();

    mui.plusReady(function() {
		//点了买入订单导航
		 document.getElementById('buy_type').addEventListener('tap', function() {
			document.querySelector('#segmentedControl1 .buybox.buy-left').className="buybox buy-left buy-active";
		    document.querySelector('#segmentedControl1 .buybox.buy-right').className="buybox buy-right";
		    document.getElementById('buy_list').style.display="block";
		    document.getElementById('sale_list').style.display="none";
		 });
		
		//点了卖出订单导航
		document.getElementById('sale_type').addEventListener('tap', function() {
			document.querySelector('#segmentedControl1 .buybox.buy-left').className="buybox buy-left";
		    document.querySelector('#segmentedControl1 .buybox.buy-right').className="buybox buy-right buy-active";
		    document.getElementById('buy_list').style.display="none";
		    document.getElementById('sale_list').style.display="block";
		});

		window.addEventListener('refresh_data',function(event){
		   refresh_data();
		});
		  
	});
    
    
    //加载数据
    function refresh_data(){
        mui.ajax({
					url: apiurl + "Trade/order_center/",
					data: {'p':1,'size':pageSize,'user_id':uid,'_token':userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						//console.log(JSON.stringify(res));
						// 请求成功
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}else {
							
							$('#buy_list #item1 ul').empty();
							$('#buy_list #item2 ul').empty();
							$('#buy_list #item3 ul').empty();
							$('#buy_list #item4 ul.saled_list').empty();
							
							$('#sale_list #item11 ul').empty();
							$('#sale_list #item22 ul').empty();
							$('#sale_list #item33 ul').empty();
							$('#sale_list #item44 ul.saled_list').empty();
							
							
							var data_1=assemble_data(res.data.buy_1.resource,1,1);
							$('#buy_list #item1 ul').append(data_1);
	
							var data_2=assemble_data(res.data.buy_2.resource,1,2);
							$('#buy_list #item2 ul').append(data_2);
							
							var data_3=assemble_data(res.data.buy_3.resource,1,3);
							$('#buy_list #item3 ul').append(data_3);
							
							var data_4=assemble_data(res.data.buy_4.resource,1,4);
							$('#buy_list #item4 ul.saled_list').append(data_4);
							
							var data_11=assemble_data(res.data.sale_1.resource,2,1);
							$('#sale_list #item11 ul').append(data_11);
	
							var data_22=assemble_data(res.data.sale_2.resource,2,2);
							$('#sale_list #item22 ul').append(data_22);
							
							var data_33=assemble_data(res.data.sale_3.resource,2,3);
							$('#sale_list #item33 ul').append(data_33);
							
							var data_44=assemble_data(res.data.sale_4.resource,2,4);
							$('#sale_list #item44 ul.saled_list').append(data_44);
							
							//console.log(JSON.stringify(res.data.sale_1));
                            buy_total_page=[res.data.buy_1.total_page,
                                res.data.buy_2.total_page,
                                res.data.buy_3.total_page,
                                res.data.buy_4.total_page];
                                
                            sale_total_page=[res.data.sale_1.total_page,
                                res.data.sale_2.total_page,
                                res.data.sale_3.total_page,
                                res.data.sale_4.total_page];
                                
						    buy_total_count=[res.data.buy_1.sum,
                                res.data.buy_2.sum,
                                res.data.buy_3.sum,
                                res.data.buy_4.sum];
                                
                            sale_total_count=[res.data.sale_1.sum,
                                res.data.sale_2.sum,
                                res.data.sale_3.sum,
                                res.data.sale_4.sum];
                                
                              /*
                              if(res.data.buy_1.sum>0) {
                              		document.querySelector('#buy_list .mui-badge').style.display="inline-block";
                              }
                              else{
                              		document.querySelector('#buy_list .mui-badge').style.display="none";
                              }
                              
                              
                              if(res.data.sale_1.sum>0) {
                              		document.querySelector('#sale_list .mui-badge').style.display="inline-block";
                              }
                              else{
                              		document.querySelector('#sale_list .mui-badge').style.display="none";
                              }
                            */
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
				
				
				
				//循环初始化所有下拉刷新，上拉加载。  买入
				mui.each(document.querySelectorAll('#buy_list .mui-scroll'), function(index, pullRefreshEl) {
						mui(pullRefreshEl).pullToRefresh({
							up: {
								callback: function() {
									var self = this;
									//clearTimeout(timer);
									setTimeout(function() {
										buy_currentPage[index]++;
										var ind=index+1;
										
										if(buy_currentPage[index] < buy_total_page[index]) {
											self.endPullUpToRefresh(false);
										}
										else{
											self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
										}
										mui.ajax({
											url: apiurl + "Trade/order_center/",
											data: {
												'p': buy_currentPage[index],
												'size': pageSize,
												'user_id': uid,
												'type':  1,
												'status': ind,
												'_token':userinfo.token
											},
											async: true,
											dataType: 'json',
											crossDomain: true, //强制使用5+跨域
											type: 'get',
											timeout: 10000,
											success: function(res) {
												console.log(JSON.stringify(res));
												// 请求成功
												if(res.status ==-3){
													mui.toast(res.msg);
													app.out();
													return false;
												}else {
													switch(ind)
													{
													case 1:
													    var data_1=assemble_data(res.data.resource,1,1);
							                            $('#buy_list #item1 ul').append(data_1);
													    break;
													case 2:
													    var data_2=assemble_data(res.data.resource,1,2);
							                            $('#buy_list #item2 ul').append(data_2);
													    break;
													case 3:
													    var data_3=assemble_data(res.data.resource,1,3);
														$('#buy_list #item3 ul').append(data_3);
													    break;
													case 4:
													    var data_4=assemble_data(res.data.resource,1,4);
														$('#buy_list #item4 ul.saled_list').append(data_4);
													    break;
													}
													
													if(buy_currentPage[index] < buy_total_page[index]) {
														self.endPullUpToRefresh(false);
													}
													else{
														self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													}
												}
											},
											error: function(xhr, type, errorThrown) {
												//console.log(JSON.stringify(xhr));
												//console.log(JSON.stringify(type));
												//console.log(JSON.stringify(errorThrown));
												mui.toast('获取数据失败！');
											}
										});
										return ;
										
									}, 500);
								}
						   }
						});
				});
				
				
				
				//循环初始化所有下拉刷新，上拉加载。 卖出
				mui.each(document.querySelectorAll('#sale_list .mui-scroll'), function(index, pullRefreshEl) {
						mui(pullRefreshEl).pullToRefresh({
							up: {
								callback: function() {
									var self = this;
									//clearTimeout(timer);
									setTimeout(function() {
										sale_currentPage[index]++;
										var ind=index+1;
										
										if(sale_currentPage[index] < sale_total_page[index]) {
											self.endPullUpToRefresh(false);
										}
										else{
											self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
										}
										mui.ajax({
											url: apiurl + "Trade/order_center/",
											data: {
												'p': sale_currentPage[index],
												'size': pageSize,
												'user_id': uid,
												'type':  2,
												'status': ind,
												'_token':userinfo.token
											},
											async: true,
											dataType: 'json',
											crossDomain: true, //强制使用5+跨域
											type: 'get',
											timeout: 10000,
											success: function(res) {
												console.log(JSON.stringify(res));
												// 请求成功
												if(res.status ==-3){
													mui.toast(res.msg);
													app.out();
													return false;
												}
												 else {
													switch(ind)
													{
													case 1:
														var data_11=assemble_data(res.data.resource,2,1);
														$('#sale_list #item11 ul').append(data_11);
													    break;
													case 2:
														var data_22=assemble_data(res.data.resource,2,2);
														$('#sale_list #item22 ul').append(data_22);
													    break;
													case 3:
														var data_33=assemble_data(res.data.resource,2,3);
														$('#sale_list #item33 ul').append(data_33);
													    break;
													case 4:
														var data_44=assemble_data(res.data.resource,2,4);
														$('#sale_list #item44 ul.saled_list').append(data_44);
													    break;
													}
													
													if(sale_currentPage[index] < sale_total_page[index]) {
														self.endPullUpToRefresh(false);
													}
													else{
														self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													}
												}
											},
											error: function(xhr, type, errorThrown) {
												//console.log(JSON.stringify(xhr));
												//console.log(JSON.stringify(type));
												//console.log(JSON.stringify(errorThrown));
												mui.toast('获取数据失败！');
											}
										});
										return ;
										
									}, 500);
								}
						   }
						});
				});
    }
    
    
    function assemble_data(data,type,status){
    	if(status==4){
    		var htm=order_list(data,type);
    		return htm;
    	}
    	/*
    	 * <p class="order_num">订单号：1231231213131</p>
						<li class="mui-table-view-cell">
							<div>
								<img class="mui-media-object mui-pull-left" src="img/logo.png">
								<div class="mui-media-body" style="margin: 0px;">
									<p>卡卡</p>
									<div class="credit">信任：<span>80%</span></div>
									<div class="credit">收款方式：<span>中国建设银行</span></div>
									<div class="credit mui-ellipsis-2">收款账号：<span>6222333344445555666</span></div>
								</div>
							</div>
							<div class="number_right " style="white-space: nowrap;">
								<span class="transaction_num">交易金额:1000</span>
								<span style="overflow: hidden;">实付金额：850.00RMB</span>
								<span>2018-06-21 12:21:22</span>
								<span class="button mui-pull-right" id="confirm" type="button">确认</span>
							</div>
						</li>
    	 */
    	var htm='';
		for(z in data){
			htm+='<p class="order_num">订单号：'+data[z]['order_sn']+'</p>';
			htm+='<li class="mui-table-view-cell">';
			htm+='<div onclick="see_detail(\''+data[z]['id']+'\','+type+')">';

			if(type==1){
				if(data[z]['user_id']>0){
					if(data[z]['sale_headimgurl'] && data[z]['sale_headimgurl'].indexOf('Uploads') !== -1){
						//var imgpath=serverurl + 'Uploads/avatar/' + data[z]['user_id'] + '.jpg';
					    if(data[z]['sale_headimgurl'].indexOf(upurl+'Uploads') !== -1){
							var imgpath=data[z]['sale_headimgurl'];
						}
						else{
							var imgpath=upurl+data[z]['sale_headimgurl'];
						}
					}
					else{
						var imgpath='img/logo.png';
					}
				}
				else{
					if(data[z]['buy_headimgurl'] && data[z]['buy_headimgurl'].indexOf('Uploads') !== -1){
						//var imgpath=serverurl + 'Uploads/avatar/' + data[z]['buy_user_id'] + '.jpg';
					    if(data[z]['buy_headimgurl'].indexOf(upurl+'Uploads') !== -1){
							var imgpath=data[z]['buy_headimgurl'];
						}
						else{
							var imgpath=upurl+data[z]['buy_headimgurl'];
						}
					}
					else{
						var imgpath='img/logo.png';
					}
				}
			}
			
			if(type==2){
				if(data[z]['buy_user_id']>0){
					if(data[z]['buy_headimgurl'] && data[z]['buy_headimgurl'].indexOf('Uploads') !== -1){
						//var imgpath=serverurl + 'Uploads/avatar/' + data[z]['buy_user_id'] + '.jpg';
					    if(data[z]['buy_headimgurl'].indexOf(upurl+'Uploads') !== -1){
							var imgpath=data[z]['buy_headimgurl'];
						}
						else{
							var imgpath=upurl+data[z]['buy_headimgurl'];
						}
					}
					else{
						var imgpath='img/logo.png';
					}
				}
				else{
					if(data[z]['sale_headimgurl'] && data[z]['sale_headimgurl'].indexOf('Uploads') !== -1){
						//var imgpath=serverurl + 'Uploads/avatar/' + data[z]['user_id'] + '.jpg';
					    if(data[z]['sale_headimgurl'].indexOf(upurl+'Uploads') !== -1){
							var imgpath=data[z]['sale_headimgurl'];
						}
						else{
							var imgpath=upurl+data[z]['sale_headimgurl'];
						}
					}
					else{
						var imgpath='img/logo.png';
					}
				}
			}
			htm += '<img class="mui-media-object mui-pull-left" src="'+imgpath+'" />';
			htm+='<div class="mui-media-body" style="margin: 0px;">';
			
			if(type==1){
				if(data[z]['user_id']>0){
					htm += '<p>'+data[z]['contacts']+'</p>';
					htm += '<div class="credit">信任：<span>100%</span></div>';
					htm += '<div class="credit">收款方式：<span>'+data[z]['bank_name']+'</span></div>';
					htm += '<div class="credit mui-ellipsis-2">收款账号：<span>'+data[z]['bank_card']+'</span></div>';
				}
				else{
					htm += '<p>'+data[z]['buy_contacts']+'</p>';
					htm += '<div class="credit">信任：<span>100%</span></div>';
					htm += '<div class="credit">打款方式：<span>'+data[z]['buy_bank_name']+'</span></div>';
					htm += '<div class="credit mui-ellipsis-2">打款账号：<span>'+data[z]['buy_bank_card']+'</span></div>';
				}
			}
			
			if(type==2){
				if(data[z]['buy_user_id']>0){
					htm += '<p>'+data[z]['buy_contacts']+'</p>';
					htm += '<div class="credit">信任：<span>100%</span></div>';
					htm += '<div class="credit">打款方式：<span>'+data[z]['buy_bank_name']+'</span></div>';
					htm += '<div class="credit mui-ellipsis-2">打款账号：<span>'+data[z]['buy_bank_card']+'</span></div>';
				}
				else{
					htm += '<p>'+data[z]['contacts']+'</p>';
					htm += '<div class="credit">信任：<span>100%</span></div>';
					htm += '<div class="credit">收款方式：<span>'+data[z]['bank_name']+'</span></div>';
					htm += '<div class="credit mui-ellipsis-2">收款账号：<span>'+data[z]['bank_card']+'</span></div>';
				}
			}
			
			htm+='</div>';
			htm+='</div>';
			htm+='<div class="number_right " style="white-space: nowrap;">';
			htm+='<span class="transaction_num" style="line-height:20px">交易金额:'+data[z]['trade_money']+'</span>';	
			htm+='<span style="overflow: hidden;">实付金额：'+data[z]['amount']+'RMB</span>';
			htm+='<span>'+data[z]['add_time']+'</span>';
			if(type==1 && data[z]['status']==2){
				htm+='<span class="button mui-pull-right do_option" data-id="'+data[z]['id']+'" data-info="打款" onclick="do_operate(\''+data[z]['id']+'\',\'打款\')" type="button">打款</span>';
			}
			if(type==2 && data[z]['status']==3){
				htm+='<span class="button mui-pull-right do_option" data-id="'+data[z]['id']+' data-info="收款" onclick="do_operate(\''+data[z]['id']+'\',\'收款\')" type="button">收款</span>';
			}
			if(data[z]['status']==1){
				htm+='<span class="button mui-pull-right do_option" data-id="'+data[z]['id']+' data-info="撤单" onclick="do_operate(\''+data[z]['id']+'\',\'撤单\')" type="button">撤单</span>';
			}
			if(data[z]['status']==4){
				htm+='<span class="button mui-pull-right" style="background-color:red;color:#fff;">交易成功</span>';
			}
			
			htm+='</div>';
			htm+='</li>';
		}
		return htm;
    }
		
	
	function order_list(data,type){
		var htm='';
		for(i in data) {
			/*
			 * <li>
							<span>500</span>
							<span>181.1216</span>
							<span>2018-05-11<br/>12:12:12</span>
						</li>
			 */
			htm+='<li>';
			htm+='<span style="line-height:40px">'+data[i]['trade_money']+'</span>';
			if(type==1){
				htm+='<span style="line-height:40px">'+data[i]['buy_has_money']+'</span>';
			}
			else{
				htm+='<span style="line-height:40px">'+data[i]['sale_has_money']+'</span>';
			}
			htm+='<span style="line-height:20px">'+data[i]['add_time']+'</span>';
			htm+='</li>';
		}
		return htm;
	}
				
			
    function see_detail(tid,type){
    	if(type==1){
	    	//打开买入订单详情
			mui.openWindow({
				id: 'buy_order.html',
				url: 'pages/transaction/buy_order.html',
				extras: {
					trade_id: tid //自定义扩展参数，可以用来处理页面间传值
				},
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
		}
    	
    	if(type==2){
    		//打开卖出订单详情
			mui.openWindow({
				id: 'buyorder.html',
				url: 'pages/transaction/buyorder.html',
				extras: {
					trade_id: tid //自定义扩展参数，可以用来处理页面间传值
				},
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
    	}
    	
    }
    
    
    function do_operate(tid,name){
	    	if(name=="打款"){
	    		//打开打款详情
				mui.openWindow({
					id: 'remit_detail.html',
					url: 'pages/order/remit_detail.html',
					extras: {
						trade_id: tid //自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
				return ;
	    	}
	    	
	    	if(name=="收款"){
	    		//打开收款详情
				mui.openWindow({
					id: 'receipt_detail.html',
					url: 'pages/order/receipt_detail.html',
					extras: {
						trade_id: tid //自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
				return ;
	    	}
	    	
	    	if(name=="撤单"){
	    		mui.ajax({
				url: apiurl + "Trade/updateTrade/",
				data: {
					'user_id': uid,
					'tid': tid,
					'type': 3,
					'_token':userinfo.token
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					// 请求成功
					if(res.status ==0) {
						mui.alert(res.msg);
					} else {
						mui.toast('取消成功');
						userinfo.a1_money = res.data.a1_money;
						userinfo.a2_money = res.data.a2_money;
						app.setState(userinfo);
						//mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
						mui.fire(plus.webview.getWebviewById('transaction.html'), 'refresh_data');
						mui.fire(plus.webview.getWebviewById('wallet.html'), 'refresh_data');
						refresh_data();
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
	    }
	    	
	}
				
				
					
					
				
			